package itbour.onetouchshow.activity.version1_6;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.canvas.CompoundPathsBean;
import itbour.onetouchshow.bean.canvas.PosBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.widget.BoundView;

/**
 * Created by Administrator on 2018/5/18.
 */

public class ShowPhotoActivity extends MVPBaseActivity {
    private static String IMAGEURL = "IMAGE_URL";
    private static String COMPOUNDPATH = "COMPOUNDPATH";
    private static String POSBEAN = "POSBEAN";

    private static String PAHTW = "PAHTW";
    private static String PAHTH = "PAHTH";
    private static String IMAGEPAHT = "IMAGEPAHT";

    @BindView(R.id.ig_show)
    ImageView igShow;
    @BindView(R.id.id_root_bound)
    RelativeLayout idRootBound;
    int w;
    int h;


    public static Intent getLaunchIntent(Context context, String imageUrl, int w, int h, int pageW, int pageH, CompoundPathsBean compoundPathsBean, PosBean posBean) {
        Intent intent = new Intent(context, ShowPhotoActivity.class);
        intent.putExtra(IMAGEURL, imageUrl);
        intent.putExtra(COMPOUNDPATH, compoundPathsBean);
        intent.putExtra(POSBEAN, posBean);
        intent.putExtra(AppConst.VIDEOW, w);
        intent.putExtra(AppConst.VIDEOH, h);
        intent.putExtra(PAHTW, pageW);
        intent.putExtra(PAHTH, pageH);
        return intent;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_show_photo;
    }

    @Override
    protected void afterCreate(Bundle bundle) {
        w = getIntent().getIntExtra(AppConst.VIDEOW, UIUtils.WH()[0]);
        h = getIntent().getIntExtra(AppConst.VIDEOH, UIUtils.WH()[0]);
        int pageW = getIntent().getIntExtra(PAHTW, UIUtils.WH()[0]);
        int pageH = getIntent().getIntExtra(PAHTH, UIUtils.WH()[0]);
        CompoundPathsBean compoundPathsBean = (CompoundPathsBean) getIntent().getSerializableExtra(COMPOUNDPATH);
        PosBean posBean = (PosBean) getIntent().getSerializableExtra(POSBEAN);
        String imageUrl = getIntent().getStringExtra(IMAGEURL);


        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) idRootBound.getLayoutParams();
        layoutParams.width = w;
        layoutParams.height = h;

        //ImageLoader.getInstance().showImage(this, imageUrl, igShow);
        Glide.with(this).applyDefaultRequestOptions(new RequestOptions().apply(new RequestOptions().skipMemoryCache(true)).placeholder(R.drawable.place_icon).fitCenter()).load(imageUrl).into(igShow);
        showBoundView(pageW, pageH, compoundPathsBean, posBean);

    }


    public void showBoundView(int pageW, int pageH, CompoundPathsBean compoundPathsBean, PosBean posBean) {

        //确定当前选中的位置
        // int pageWidth = pageW;
        //int pageHeight = pageH;
        float scaleW = w * 1.0f / pageW;
        float scaleH = h * 1.0f / pageH;

        float w = compoundPathsBean.getW() * scaleW;
        float h = compoundPathsBean.getH() * scaleH;
        float x = posBean.getX() * scaleW;
        float y = posBean.getY() * scaleH;
        BoundView pathsView = new BoundView(this);
        pathsView.setId(R.id.video_edit_bound);
        pathsView.setData(compoundPathsBean, (int) w, (int) h);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) w, (int) h);
        lp.leftMargin = (int) x;
        lp.topMargin = (int) y;
        idRootBound.addView(pathsView, lp);
    }


    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return super.onTouchEvent(event);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }
}
